import matplotlib

from graphshow import Ui_PlotGraph
from PyQt5.QtWidgets import (QMainWindow, QRadioButton, QGraphicsScene,
                             QGraphicsPixmapItem, QFileDialog, QMessageBox)
from PyQt5.QtGui import QImage, QPixmap


class GraphShow(QMainWindow, Ui_PlotGraph):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent  # RunPlt类传入run_plot.py >>> prov = Province(self)
        self.setupUi(self)
        self.scale = 1  # 图片放缩尺度
        self.scene = QGraphicsScene()  # 创建场景
        self.btn_littler.clicked.connect(self.zoom_l)
        self.btn_bigger.clicked.connect(self.zoom_b)
        self.btn_cancel_zoom.clicked.connect(self.zoom_one)
        self.btn_prov_summary.clicked.connect(self.prov_summary)
        self.btn_prov_by_year.clicked.connect(self.pie_by_year)
        self.btn_monthly.clicked.connect(self.line_mth_hty)
        self.btn_inv.clicked.connect(self.line_inv)
        self.btn_share.clicked.connect(self.bar_share)
        self.btn_rank.clicked.connect(self.bar_rank)
        self.btn_districts_corr.clicked.connect(self.bar_dis_corr)
        self.btn_savepic.clicked.connect(self.save_picture)

    def _get_params(self):
        self.diff = self.check_diff.isChecked()
        self.single = self.chk_single.isChecked()
        result = []
        for grp in [self.grp_ptt_appr, self.grp_ptt_type, self.grp_districts]:
            for btn in grp.findChildren(QRadioButton):
                if btn.isChecked():
                    result.append(btn.text())
                    break
        result.append('')  # 地区可能暂时为空, 可以用空字符串代替
        self.ptt_appr, self.patent_type, self.district, *_ = result

    def _plot2graphview(self, func, *args, **kwargs):
        img = func(*args, **kwargs)
        self.img = img
        x = img.shape[1]
        y = img.shape[0]
        frame = QImage(img, x, y, QImage.Format_RGB888)
        pix = QPixmap.fromImage(frame)
        self.item = QGraphicsPixmapItem(pix)  # 创建像素图元
        self.scene.clear()
        self.scene.addItem(self.item)
        self.graphview.setScene(self.scene)

    def prov_summary(self):
        self._get_params()
        self._plot2graphview(self.parent.ah_ptt.bar_prov_summary,
                             self.ptt_appr,
                             patent_type=self.patent_type,
                             diff=self.diff)

    def pie_by_year(self):
        self._get_params()
        try:
            self._plot2graphview(self.parent.ah_ptt.pie_prov_prop,
                                 self.ptt_appr,
                                 patent_type=self.patent_type,
                                 year=self.combo_year.currentText())
        except ValueError:
            QMessageBox.warning(self, '操作失败', '缺少当前年份数据！')

    def line_mth_hty(self):
        self._plot2graphview(self.parent.ah_ptt.line_monthly_history)

    def line_inv(self):
        self._get_params()
        self._plot2graphview(self.parent.ah_ptt.line_invention,
                             patent_type=self.patent_type)

    def bar_share(self):
        self._get_params()
        self._plot2graphview(self.parent.ah_ptt.bars_annual_share,
                             patent_type=self.patent_type)

    def bar_rank(self):
        self._get_params()
        self._plot2graphview(self.parent.ah_ptt.annual_rank,
                             self.ptt_appr,
                             patent_type=self.patent_type)

    def bar_dis_corr(self):
        self._get_params()
        self._plot2graphview(self.parent.ah_ptt.bars_district_corr,
                             patent_type=self.patent_type,
                             single=self.single,
                             ptt_appr=self.ptt_appr,
                             district=self.district)

    def save_picture(self):
        if self.img is not None:
            # options = QFileDialog.Options()
            # options |= QFileDialog.DontUseNativeDialog
            path = QFileDialog.getSaveFileName(None,
                                               "选择要存放的位置并指定或写入文件名",
                                               "", "Image files (*.png)",
                                               # options=options
                                               )[0]
            if path:
                matplotlib.image.imsave(path, self.img)
                QMessageBox.information(self, '完成提示!', '图片已保存至:{}'.format(path))

    def zoom_l(self):
        if self.item:
            self.scale -= 0.1
            self.item.setScale(self.scale)

    def zoom_b(self):
        if self.item:
            self.scale += 0.1
            self.item.setScale(self.scale)

    def zoom_one(self):
        if self.item:
            self.scale = 1
            self.item.setScale(self.scale)


